package com.travel.travel_springboot_web.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class TokenUtil {
    
    private static final Logger logger = LoggerFactory.getLogger(TokenUtil.class);
    
    public String extractToken(String authHeader) {
        try {
            if (authHeader == null) {
                logger.warn("Authorization header is null");
                throw new RuntimeException("请先登录");
            }
            
            // 如果已经是token本身（没有Bearer前缀）
            if (!authHeader.startsWith("Bearer ")) {
                logger.debug("Using raw token: {}", authHeader);
                return authHeader;
            }
            
            // 如果有Bearer前缀，去掉前缀
            String token = authHeader.substring(7);
            logger.debug("Extracted token from Bearer: {}", token);
            return token;
        } catch (Exception e) {
            logger.error("Error extracting token: ", e);
            throw new RuntimeException("请先登录");
        }
    }
} 